require( '$d/css/common.less' );
require( './style.less' );
define( [
  'jquery',
  'require',
  'header',
  'message',
  'api',
  'bigwe_utils',
  'artTemplate'
], function( $, require ) {

  var msg = require( 'message' );
  var bu = require( 'bigwe_utils' );
  var at = require( 'artTemplate' );
  var thisApi = require( 'api' ).GATHER;

  $( '.bw-columns' ).bwColumns( [ '单个提交', '批量采集' ], null, '.addcollect_lower' );

  $( document ).on( 'click', '.platform_selec span', function() {
    var _idx = $( this ).index();
    $( this ).addClass( 'current' ).siblings().removeClass( 'current' );
    $( '.platform_cont' ).eq( _idx ).addClass( 'show' ).siblings().removeClass( 'show' );
  } ).on( 'click', '#choose_file,#ipt_file_name', function() {
    $( '#inp_template' ).trigger( 'click' );
  } ).on( 'change', '#inp_template', function() {
    var lastIndexof = this.value.lastIndexOf( '\\' );
    var $frm = $( '#importTemplate' );
    $( '#ipt_file_name' ).val( this.value.substr( lastIndexof + 1, this.value.length ) );

    $frm.ajaxSubmit( {
      type: 'post',
      url: thisApi.ADD_BATCH_ACCOUNT,
      success: function( data ) {
        $( '#progress_text' ).text( '上传完成！' );
        data = typeof data === 'string' ? $.parseJSON( data ) : data;
        if ( data.ret === 0 ) {
          $( '#import_result_container' ).html( at( 'import_result_html', data.data ) );

          if ( !data.data.errors || !data.data.errors.length ) {
            $( '.result_detal tbody' ).append( '<tr><td colspan="2">未解析到有效行</td></tr>' );
          }

        } else {
          msg.showWarning( data.msg, msg.showTime );
        }
      },
      error: function() {
        $( '#progress_text' ).html( '<span class="red">文档解析失败，请检查文档格式！</span>' );
      },
      complete: function() {
        $frm[ 0 ].reset();
      },
      beforeSubmit: function() {
        $( '#progress_text' ).text( '上传中' );
        $( '.progress_container .inner_progress' ).width( 0 );
        $( '#progress' ).text( '0%' );
        return true;
      },
      uploadProgress: function( event, position, total, percent ) {
        $( '.progress_container .inner_progress' ).css( 'width', percent + '%' );
        $( '#progress' ).text( percent + '%' );
      }
    } );
  } ).on( 'submit', '.platform_cont form', function() {
    // 获取当前提交的平台
    var platform = $( this ).data( 'platform' );
    var name = $.trim( $( '[name="name"]', this ).val() );
    var history_link = $.trim( $( '[name="history_link"]', this ).val() );


    if ( name === '' ) {
      msg.showWarning( '请输入待采集的平台的账号名称', msg.showTime );
      return false;
    }

    try {
      var url = new URL( history_link );
      if ( url.host.indexOf( platform ) < 0 ) {
        msg.showWarning( '您输入的地址和当前平台不匹配', msg.showTime );
        return false;
      }
    } catch ( e ) {
      msg.showWarning( '请输入一个正确的url', msg.showTime );
      return false;
    }

    // 提交
    $( '[type="submit"]', this ).prop( 'disabled', 'disabled' );
    $.post( thisApi.ADD_ACCOUNT, $( this ).serialize(), function( data ) {
      data = typeof data === 'string' ? $.parseJSON( data ) : data;
      if ( data.ret === 0 ) {
        msg.show( '提交成功，请耐心等待审核:)', msg.showTime );
        this.reset();
      } else {
        msg.showWarning( data.msg, msg.showTime );
      }
    }.bind( this ) ).always( function() {
      $( '[type="submit"]', this ).removeAttr( 'disabled' );
    }.bind( this ) );

    return false;
  } );
} );
